﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDOS.Objects;
using Microsoft.Xna.Framework;


namespace DDOS
{
    class CollisionEngine
    {
        private static CollisionEngine instance;
        public static CollisionEngine Instance { get { return instance; } }

        List<BoundingObject> collisionObjects;
        public static void Initiate()
        {
            instance = new CollisionEngine();
            instance.collisionObjects = new List<BoundingObject>();
        }

        public void Add(BoundingObject bo)
        {
            this.collisionObjects.Add(bo);
        }

        public void Remove(BoundingObject bo)
        {
           this. collisionObjects.Remove(bo);
        }


        public void Update(GameTime gt)
        {
            for (int i = instance.collisionObjects.Count-1; i >= 0; i--)
            {
                for (int j = instance.collisionObjects.Count-1; j >= 0; j--)
                {
                    if(i!=j)
                    {
                        try
                        {
                            if(collisionObjects[i].Intersects(collisionObjects[j]))
                            {
                            
                                collisionObjects[i].parent.CollisionCallback(collisionObjects[j], collisionObjects[i]);
                            
                            }
                        }
                        catch (Exception e)
                        {
                            //Console.WriteLine("Exception " + e + "\n Collision between " + instance.collisionObjects[i].parent + " and " + instance.collisionObjects[j].parent);
                        }
                    }
                }
            }
        }

    }
}
